/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.session;

import edu.manchester.biology.eqhei.business.StreamReachBusiness;
import edu.manchester.biology.eqhei.persistence.StreamReach;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author rrmitchell
 */
@Stateless
public class StreamReachBean implements StreamReachBeanRemote {
    private EntityManagerFactory    entityManagerFactory = Persistence.createEntityManagerFactory("Alspark-ejbPU");
    private EntityManager           entityManager;
    private StreamReachBusiness streamReachReturn = null;

    @Override
    public StreamReachBusiness getStreamReach(String UTM) {
        try
        {
            System.out.println("inside getStreamReach" + " " + UTM);
            entityManager = entityManagerFactory.createEntityManager();
            if ( entityManager == null )
            {
                System.out.println("entity manager is null");
                return null;
            }
            StreamReach streamReach = entityManager.find(StreamReach.class, UTM);
         
            if ( streamReach == null )
            {
                System.out.println("returned null stream reach");
                return null;
            }
            String utm = streamReach.getUtm();
            System.out.println(utm);
            String alias = streamReach.getAlias();
            System.out.println(alias);
            String stream = streamReach.getStream();
            System.out.println(stream);
            streamReachReturn = new StreamReachBusiness(utm, alias, stream);
            System.out.println(streamReachReturn.getAlias());
            
        }
        catch(Exception e)
        {
            System.out.println("***** " + e.getMessage() + " *****");
            return null;
        }
        finally{
            entityManager.close();
            
        }
        
        
        return streamReachReturn;
        
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void createStreamReach(String UTM, String alias, String stream) {
        try
        {
            System.out.println("inside createStreamReach"  + UTM + " " + alias + " " + stream);
            entityManager = entityManagerFactory.createEntityManager();
            if ( entityManager == null )
            {
                System.out.println("entity manager is null");
                return;
            }
            //StreamReach streamReach = entityManager.find(StreamReach.class, UTM);
            //if ( streamReach == null )
            //{
            //    System.out.println("returned null stream reach");
            //    return;
            //}
            StreamReach streamReach = new StreamReach();
            streamReach.setUtm(UTM);
            streamReach.setAlias(alias);
            streamReach.setStream(stream);
            System.out.println("flushing");
            entityManager.getTransaction().begin();
            entityManager.persist(streamReach);
            entityManager.getTransaction().commit();
            
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            throw e;
        }
        finally
        {
            entityManager.close();            
        }
        
        
    }
}
